/**
 * Created by S on 2016/5/5.
 */
var DBTool=require('commom').DBTool;

exports.insertcontent=function(condition,callback){
    var sql="insert into commodity(name,code,costprice,size,unit,typeid,textureid,colorid,sex,produce,addadmin,addtime,remarks) values('" + condition.name +"','"+condition.code +"'," + condition.costprice + "," + condition.size + ",'" + condition.unit+","+condition.typeid+","+condition.textureid+","+condition.colorid+","+condition.sex+",'"+condition.produce+"','"+condition.addadmin+"','"+condition.addtime+"','"+condition.remarks +"')";
    var params=[];
    DBTool.Execute(sql,params,function(err,data){
        if(err)
        {
            callback(err,null);
        }
        else
        {
            callback(null,data);
        }
    });
}

exports.findcontent = function (condition,callback) {
    var star=false;
    var c="where";
    var sql="select * from commodity ";
    if(condition.id!=undefined){
        if(star)
            c=c+" and ";
        c=c+" id = "+condition.id;
        star=true;
    }
    if(condition.name!=undefined){
        if(star)
            c=c+" and ";
        c=c+" name = '"+condition.name+"'";
        star=true;
    }
    if(condition.code!=undefined){
        if(star)
            c=c+" and ";
        c=c+" code = '"+condition.code+"'";
        star=true;
    }
    if(c!="where"){
        sql=sql + c;
    }
    var params=[];
    DBTool.Execute(sql,params, function ( err, data ) {
        if(err)
        {
            callback(err,null);
        }
        else
        {
            callback(null,data);
        }
    });
};

exports.changecontent=function(condition,callback){
    var sql="update commodity set ";
    var start=false;
    if(condition.name!=undefined)
    {
        if(start)
            sql=sql+",";
        sql=sql+" name = '"+condition.name+"'";
    }
    if(condition.code!=undefined)
    {
        if(start)
            sql=sql+",";
        sql=sql+" code = '"+condition.code+"'";
    }
    if(condition.costprice!=undefined)
    {
        if(start)
            sql=sql+",";
        sql=sql+" costprice = "+condition.costprice;
    }
    if(condition.size!=undefined)
    {
        if(start)
            sql=sql+",";
        sql=sql+" size = "+condition.size;
    }
    if(sql=="update commodity set ")
    {
        cb('未做任何修改',null);
    }
    else
    {
        sql=sql+" where id = "+condition.id;
    }
    var params=[];
    DBTool.Execute(sql,params,function(err,data){
        if(err)
        {
            callback(err,null);
        }
        else
        {
            callback(null,data);
        }
    });
}

exports.deletecontent=function(condition,callback){
    var sql="delete commodity";
    if(condition.id!=undefined){
        sql=sql+" where id = "+condition.id;
    }
    var params=[];
    DBTool.Execute(sql,params,function(err,data){
        if(err)
        {
            callback(err,null);
        }
        else
        {
            callback(null,data);
        }
    });
}